package com.lmk.org.controller;

import com.lmk.basic.util.AjaxResult;
import com.lmk.basic.util.PageList;
import com.lmk.org.domain.Employee;
import com.lmk.org.query.EmployeeQuery;
import com.lmk.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired// 自动注入
    private EmployeeService service;

    @GetMapping// 查询所有
    public List<Employee> findAll(){
        List<Employee> list = service.findAll();
        return list;
    }

    // 查询单个
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id") Long id){
        Employee byId = service.findById(id);
        return byId;
    }

    // 分页查询
    @PostMapping
    public PageList<Employee> querPage(@RequestBody EmployeeQuery query){
        PageList<Employee> employeePageList = service.queryPage(query);
        return employeePageList;
    }

    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            service.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxResult(false,"删除失败");
    }
    // 添加或修改
    @PutMapping
    public AjaxResult addorupdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                service.add(employee);
            }else {
                service.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
            return new AjaxResult(false,"操作失败");
    }
    @PatchMapping// 批量删除
    public AjaxResult batchdel(@RequestBody List<Long> ids){
        try {
            service.batchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }
    @GetMapping(value="/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id") Long id){
        List<Employee> employees = service.findByShopId(id);
        return employees;
    }

}
